package edu.uoc.tdp.pac4.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.event.ActionListener;

import edu.uoc.tdp.pac4.common.ConnectionRMI;
import edu.uoc.tdp.pac4.common.GestorExceptions;
import edu.uoc.tdp.pac4.common.RemotInterface;
import edu.uoc.tdp.pac4.i18n.TDSLanguageUtils;

import java.awt.Color;


public class test extends JFrame implements ActionListener{

	private JFrame frame;

	private JTextField nombre;

	private JButton botonEnviar;
	
	private JButton botonrec;

	private static RemotInterface rmiServidor;

	private JLabel lblError; 
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					TDSLanguageUtils.setDefaultLanguage("edu.uoc.tdp.pac4.i18n/messages");
					
					conectarseAlServidor();
					test frame = new test();
					frame.frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private static void conectarseAlServidor() {
			rmiServidor = ConnectionRMI.conectarseAlServidor();
	}

	private boolean enviarMensaje(ActionEvent e) {
		try {
			// llamando el metodo remoto
			return rmiServidor.recibirMensaje(nombre.getText());
		} catch (RemoteException re) {
			GestorExceptions.sentException(GestorExceptions.ERR_URL);
		}catch (Exception re) {
			GestorExceptions.sentException(GestorExceptions.ERR_URL);
		}
		return false;
	}

	
	/**
	 * Create the frame.
	 */
	public test() {
		frame = new JFrame();
		frame.setBounds(100, 100, 397, 173);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		botonEnviar = new JButton("Enviar");
		botonEnviar.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent evt) {
            	enviarMensaje(evt);
            }
        });
		botonEnviar.setBounds(273, 110, 100, 20);
		panel.add(botonEnviar);
		
		
		botonrec = new JButton("Reconect");
		botonrec.addActionListener(new ActionListener() {
            @Override
			public void actionPerformed(ActionEvent evt) {
            	conectarseAlServidor();
            }
        });
		botonrec.setBounds(150, 110, 100, 20);
		panel.add(botonrec);
		
		nombre = new JTextField();
		nombre.setBounds(78, 11, 295, 20);
		nombre.addActionListener(this);
		panel.add(nombre);
		
		JLabel lblNom = new JLabel("Texto");
		lblNom.setBounds(10, 14, 46, 14);
		panel.add(lblNom);
		
		lblError = new JLabel("");
		lblError.setForeground(Color.RED);
		lblError.setBounds(49, 113, 214, 14);
		panel.add(lblError);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
